<?php

namespace app\common\model;
class Setting extends Common
{
	public $skeys = [
		'shop_name'                 => [
			'name'  => '平台名称',
			'value' => 'jshop商城系统'
		],
		'shop_desc'                 => [
			'name'  => '平台描述',
			'value' => '平台描述会展示在前台及微信分享店铺描述'
		],
		'shop_address'              => [
			'name'  => '平台地址',
			'value' => '我的平台地址'
		],
		'shop_company'                => [
			'name'  => '公司名称',
			'value' => '湖南万学网络科技有限公司'
		],
		'shop_beian'                => [
			'name'  => '备案信息',
			'value' => '网站备案信息'
		],
		'shop_gzh'                 => [
			'name'  => '平台公众号',
			'value' => '',
		],
		'shop_sph'                 => [
			'name'  => '平台视频号',
			'value' => '',
		],
		'shop_logo'                 => [
			'name'  => '平台logo',
			'value' => '',
		],
		'shop_ico'              => [
			'name'  => '平台icon',
			'value' => '',
		],
		'shop_default_image'        => [
			'name'  => '默认图',
			'value' => '',
		],
		'shop_mobile'               => [
			'name'  => '商家手机号',
			'value' => ''
		],
		'product_sparkle_p1'        => [
			'name'  => '内容1',
			'value' => ''
		],
		'product_sparkle_p2'        => [
			'name'  => '内容2',
			'value' => ''
		],
		'product_sparkle_p3'        => [
			'name'  => '内容3',
			'value' => ''
		],
		'product_sparkle_p4'        => [
			'name'  => '内容4',
			'value' => ''
		],
		'product_sparkle_p5'        => [
			'name'  => '内容5',
			'value' => ''
		],
		'product_sparkle_p6'        => [
			'name'  => '内容6',
			'value' => ''
		],
		'product_sparkle_p7'        => [
			'name'  => '内容7',
			'value' => ''
		],
		'product_sparkle_p8'        => [
			'name'  => '内容8',
			'value' => ''
		],
		'product_sparkle_p9'        => [
			'name'  => '内容9',
			'value' => ''
		],
		'product_sparkle_pic'       => [
			'name'  => '图片',
			'value' => ''
		],
		'product_character_desc'    => [
			'name'  => '描述',
			'value' => ''
		],
		'product_character_p1'      => [
			'name'  => '内容1',
			'value' => ''
		],
		'product_character_pic1'    => [
			'name'  => '图片1',
			'value' => ''
		],
		'product_character_desc1'   => [
			'name'  => '描述1',
			'value' => ''
		],
		'product_character_p2'      => [
			'name'  => '内容2',
			'value' => ''
		],
		'product_character_pic2'    => [
			'name'  => '图片2',
			'value' => ''
		],
		'product_character_desc2'   => [
			'name'  => '描述2',
			'value' => ''
		],
		'product_superiority_t1'    => [
			'name'  => '标题1',
			'value' => ''
		],
		'product_superiority_desc1' => [
			'name'  => '标注1',
			'value' => ''
		],
		'product_superiority_t2'    => [
			'name'  => '标题2',
			'value' => ''
		],
		'product_superiority_desc2' => [
			'name'  => '标注2',
			'value' => ''
		],
		'product_superiority_t3'    => [
			'name'  => '标题3',
			'value' => ''
		],
		'product_superiority_desc3' => [
			'name'  => '标注3',
			'value' => ''
		],
		'product_merit_p1'          => [
			'name'  => '内容1',
			'value' => ''
		],
		'product_merit_pic1'        => [
			'name'  => '图片1',
			'value' => ''
		],
		'product_merit_desc1'       => [
			'name'  => '描述1',
			'value' => ''
		],
		'product_merit_p2'          => [
			'name'  => '内容2',
			'value' => ''
		],
		'product_merit_pic2'        => [
			'name'  => '图片2',
			'value' => ''
		],
		'product_merit_desc2'       => [
			'name'  => '描述2',
			'value' => ''
		],
		'product_advantage_t1'      => [
			'name'  => '内容1',
			'value' => ''
		],
		'product_advantage_pic1'    => [
			'name'  => '图片1',
			'value' => ''
		],
		'product_advantage_desc1'   => [
			'name'  => '描述1',
			'value' => ''
		],
		'product_advantage_t2'      => [
			'name'  => '内容2',
			'value' => ''
		],
		'product_advantage_pic2'    => [
			'name'  => '图片2',
			'value' => ''
		],
		'product_advantage_desc2'   => [
			'name'  => '描述2',
			'value' => ''
		],
		'product_advantage_t3'      => [
			'name'  => '内容3',
			'value' => ''
		],
		'product_advantage_pic3'    => [
			'name'  => '图片3',
			'value' => ''
		],
		'product_advantage_desc3'   => [
			'name'  => '描述3',
			'value' => ''
		],
		'product_ability_t1'        => [
			'name'  => '标题1',
			'value' => ''
		],
		'product_ability_icon1'     => [
			'name'  => '图标icon1',
			'value' => ''
		],
		'product_ability_pic1'      => [
			'name'  => '图标pic1',
			'value' => ''
		],
		'product_ability_img1'      => [
			'name'  => '图片img1',
			'value' => ''
		],
		'product_ability_desc1'     => [
			'name'  => '描述1',
			'value' => ''
		],
		'product_ability_t2'        => [
			'name'  => '标题2',
			'value' => ''
		],
		'product_ability_icon2'     => [
			'name'  => '图标icon2',
			'value' => ''
		],
		'product_ability_pic2'      => [
			'name'  => '图标pic2',
			'value' => ''
		],
		'product_ability_img2'      => [
			'name'  => '图片img2',
			'value' => ''
		],
		'product_ability_desc2'     => [
			'name'  => '描述2',
			'value' => ''
		],
		'product_ability_t3'        => [
			'name'  => '标题3',
			'value' => ''
		],
		'product_ability_icon3'     => [
			'name'  => '图标icon3',
			'value' => ''
		],
		'product_ability_pic3'      => [
			'name'  => '图标pic3',
			'value' => ''
		],
		'product_ability_img3'      => [
			'name'  => '图片img3',
			'value' => ''
		],
		'product_ability_desc3'     => [
			'name'  => '描述3',
			'value' => ''
		],
		'product_ability_t4'        => [
			'name'  => '标题4',
			'value' => ''
		],
		'product_ability_icon4'     => [
			'name'  => '图标icon4',
			'value' => ''
		],
		'product_ability_pic4'      => [
			'name'  => '图标pic4',
			'value' => ''
		],
		'product_ability_img4'      => [
			'name'  => '图片img4',
			'value' => ''
		],
		'product_ability_desc4'     => [
			'name'  => '描述4',
			'value' => ''
		],
		'product_ability_t5'        => [
			'name'  => '标题5',
			'value' => ''
		],
		'product_ability_icon5'     => [
			'name'  => '图标icon5',
			'value' => ''
		],
		'product_ability_pic5'      => [
			'name'  => '图标pic5',
			'value' => ''
		],
		'product_ability_img5'      => [
			'name'  => '图片img5',
			'value' => ''
		],
		'product_ability_desc5'     => [
			'name'  => '描述5',
			'value' => ''
		],
		'product_ability_t6'        => [
			'name'  => '标题6',
			'value' => ''
		],
		'product_ability_icon6'     => [
			'name'  => '图标icon6',
			'value' => ''
		],
		'product_ability_pic6'      => [
			'name'  => '图标pic6',
			'value' => ''
		],
		'product_ability_img6'      => [
			'name'  => '图片img6',
			'value' => ''
		],
		'product_ability_desc6'     => [
			'name'  => '描述6',
			'value' => ''
		],
		'image_storage_type'        => [
			'name'  => '图片存储引擎',
			'value' => 'Local'
		],
		'image_storage_params'      => [
			'name'  => '图片存储配置参数',
			'value' => ''
		],
	];

	//设置参数
	public function setValue($skey, $value)
	{
		$result = $this->check($skey, $value);
		if (! $result['status']) {
			return $result;
		}
		if (is_array($value)) {
			$value = json_encode($value);
		}
		$info = $this->where(array('skey' => $skey))->find();
		if ($info) {
			$info->value = $value;
			$info->save();
		} else {
			$model = new $this;
			$model->save([
							 'skey'  => $skey,
							 'value' => $value
						 ]);
		}
		$result['status'] = true;
		return $result;
	}

	//取得参数
	public function getValue($skey)
	{
		$info = $this->where(array('skey' => $skey))->find();
		if ($info) {
			if (isjson($info['value'])) {
				$info['value'] = json_decode($info['value'], true);
			}
			return $info['value'];
		} else {
			if (isset($this->skeys[$skey]['value'])) {
				return $this->skeys[$skey]['value'];
			} else {
				return "";
			}
		}
	}

	/**
	 * 一次查询获取多个配置信息
	 * @param $skeys
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function getMultipleValue($skeys)
	{
		$skeys = explode(',', $skeys);
		$newList = [];
		//默认赋值
		foreach ($skeys as $v) {
			$val = '';
			if (isset($this->skeys[$v]['value'])) {
				$val = $this->skeys[$v]['value'];
			}
			$newList[$v] = $val;
		}
		//存储赋值
		$where[] = [
			'skey',
			'in',
			$skeys
		];
		$list = $this->where($where)->cache(true)->select();
		foreach ($list as $v) {
			if (isjson($v['value'])) {
				$v['value'] = json_decode($v['value'], true);
			}
			$newList[$v['skey']] = $v['value'];
		}
		return $newList;
	}

	//参数校验
	public function check($skey, $value)
	{
		if (! isset($this->skeys[$skey])) {
			return error_code(10008);
		}
		if ($skey == 'shop_name') {
			if ($value == '') {
				//                $result['msg'] = "平台名称不能为空";
				return error_code(10084);
			}
		}
		if ($skey == 'shop_mobile') {
			if ($value != '') {
				if (! isMobile($value)) {
					//                    $result['msg'] = '联系方式号码格式错误';
					return error_code(10085);
				}
			}
		}
		$result = [
			'status' => true,
			'data'   => '',
			'msg'    => ''
		];
		return $result;
	}

	//取得全部参数
	public function getAll()
	{
		$list = $this->select();
		foreach ($this->skeys as $k => $v) {
			foreach ($list as $info) {
				if ($info['skey'] == $k) {
					if (isjson($info['value'])) {
						$info['value'] = json_decode($info['value'], true);
					}
					$this->skeys[$k]['value'] = $info['value'];
					break;
				}
			}
		}
		return $this->skeys;
	}

	/**
	 * 返回layui的table所需要的格式
	 * @param $post
	 * @return mixed
	 * @author sin
	 */
	public function tableData($post)
	{
		if (isset($post['limit'])) {
			$limit = $post['limit'];
		} else {
			$limit = config('paginate.list_rows');
		}
		$tableWhere = $this->tableWhere($post);
		$list = $this->field($tableWhere['field'])->where($tableWhere['where'])->order($tableWhere['order'])->paginate($limit);
		$data = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
		$re['code'] = 0;
		$re['msg'] = '';
		$re['count'] = $list->total();
		$re['data'] = $data;
		//        $re['sql'] = $this->getLastSql();
		return $re;
	}

	/**
	 * 根据输入的查询条件，返回所需要的where
	 * @param $post
	 * @return mixed
	 * @author sin
	 */
	protected function tableWhere($post)
	{
		$where = [];
		if (isset($post['skey']) && $post['skey'] != "") {
			$where[] = [
				'skey',
				'eq',
				$post['skey']
			];
		}
		if (isset($post['value']) && $post['value'] != "") {
			$where[] = [
				'value',
				'like',
				'%' . $post['value'] . '%'
			];
		}
		$result['where'] = $where;
		$result['field'] = "*";
		$result['order'] = [];
		return $result;
	}

	/**
	 * 根据查询结果，格式化数据
	 * @param $list
	 * @return mixed
	 * @author sin
	 */
	protected function tableFormat($list)
	{
		foreach ($list as $k => $v) {
			if (isset($this->skeys[$v['skey']])) {
				$list[$k]['key_name'] = $this->skeys[$v['skey']]['name'];
			} else {
				$list[$k]['key_name'] = "";
			}
		}
		return $list;
	}
}
